﻿using Microsoft.Extensions.Logging;
using RBAC.MaHaiBo.Entity;
using RBAC.MaHaiBo.Entity.Traceability;
using RBAC.MaHaiBo.Repository.Interface.TraceabilityInterface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace RBAC.MaHaiBo.Service.TraceabilityService
{
    /// <summary>
    /// 养殖服务层
    /// </summary>
    public class FarmService: IFarmService
    {
        /// <summary>
        /// 日志
        /// </summary>
        ILogger<FarmService> logger;
        /// <summary>
        /// 保健
        /// </summary>
        IHygieneRepository HygieneR;
        /// <summary>
        /// 养殖
        /// </summary>
        IFarmRepository FarmR;
        /// <summary>
        /// 企业
        /// </summary>
        IFirmRepository FirmR;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="logger">日志</param>
        /// <param name="HygieneR">保健</param>
        /// <param name="FarmR">养殖</param>
        /// <param name="FirmR">企业</param>
        public FarmService(ILogger<FarmService> logger,
        IHygieneRepository HygieneR,
        IFarmRepository FarmR,
        IFirmRepository FirmR)
        {
            this.logger = logger;
            this.HygieneR = HygieneR;
            this.FarmR = FarmR;
            this.FirmR = FirmR;
        }
        /// <summary>
        /// 添加养殖信息
        /// </summary>
        /// <param name="farm">要添加数据信息</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddFarm(Farm farm)
        {
            try
            {
               return await FarmR.AddAsync(farm);
            }
            catch (Exception ex)
            {
                logger.LogError("【养殖服务层】添加养殖信息 出错"+ ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 显示养殖信息
        /// </summary>
        /// <param name="LOT">生产批次</param>
        /// <returns></returns>
        public async Task<List<Farm>> FarmInfo(string LOT)
        {
            try
            {
                var list = await FarmR.GetAsync(p => p.LOT == LOT);
                return list;
            }
            catch (Exception ex)
            {
                logger.LogError("【养殖服务层】显示养殖信息 出错"+ ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 修改养殖信息
        /// </summary>
        /// <param name="farm">修改后的养殖信息</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UpdFarm(Farm farm)
        {
            try
            {
                return await FarmR.UpdateAsync(farm);
            }
            catch (Exception ex)
            {
                logger.LogError("【养殖服务层】修改养殖信息 出错"+ ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 添加保健信息
        /// </summary>
        /// <param name="hygiene">要添加的数据信息</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddHygiene(Hygiene hygiene)
        {
            try
            {
                return await HygieneR.AddAsync(hygiene);
            }
            catch (Exception ex)
            {
                logger.LogError("【养殖服务层】添加保健信息 出错"+ ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 显示保健信息列表
        /// </summary>
        /// <returns>返回数据信息集合</returns>
        public async Task<List<Hygiene>> ListHygiene(string? LOT)
        {
            try
            {
                return await HygieneR.GetAsync(p=>p.LOT== LOT);
                
            }
            catch (Exception ex)
            {
                logger.LogError("【养殖服务层】显示保健信息列表 出错"+ ex.Message);
                throw;
            }
        }
        

        /// <summary>
        /// 反填保健信息
        /// </summary>
        /// <param name="Hygieneid">保健信息编号</param>
        /// <returns>返回一条保健信息</returns>
        public async Task<Hygiene> HygieneInfo(int Hygieneid)
        {
            try
            {
                return await HygieneR.GetModelAsync(Hygieneid);

            }
            catch (Exception ex)
            {
                logger.LogError("【养殖服务层】反填保健信息 出错"+ ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 修改保健信息
        /// </summary>
        /// <param name="Hygiene">修改后的保健信息</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UpdHygiene(Hygiene Hygiene)
        {
            try
            {
                return await HygieneR.UpdateAsync(Hygiene);

            }
            catch (Exception ex)
            {
                logger.LogError("【养殖服务层】修改保健信息 出错"+ ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 删除保健信息
        /// </summary>
        /// <param name="Hygieneid">要删除的保健信息编号</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> DelHygiene(int Hygieneid)
        {
            try
            {
              var hyginfo= await HygieneR.GetModelAsync(Hygieneid);

                return await HygieneR.DeleteAsync(hyginfo);

            }
            catch (Exception ex)
            {
                logger.LogError("【养殖服务层】删除保健信息 出错"+ ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 添加企业部分信息
        /// </summary>
        /// <param name="farmFirm">养殖场</param>
        /// <param name="principal">负责人</param>
        /// <param name="NumberOfFields">存栏数量</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> AddFirm(string? farmFirm, string? principal, int NumberOfFields, string? LOT)
        {
            try
            {
                var firm = new Firm();
                firm.LOT = LOT;
                firm.farmFirm = farmFirm;
                firm.principal = principal;
                firm.NumberOfFields = NumberOfFields;
                return await FirmR.AddAsync(firm);

            }
            catch (Exception ex)
            {
                logger.LogError("【养殖服务层】添加企业部分信息 出错"+ ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 修改企业部分信息
        /// </summary>
        /// <param name="farmFirm">养殖场</param>
        /// <param name="principal">负责人</param>
        /// <param name="NumberOfFields">存栏数量</param>
        /// <returns>返回受影响行数</returns>
        public async Task<int> UPdirm(string? farmFirm, string? principal, int NumberOfFields, string? LOT)
        {
            try
            {

                var firm = new Firm();
                var list = await FirmR.GetAsync(p => p.LOT == LOT);
                foreach (var item in list)
                {
                    firm = item;
                }
                firm.farmFirm = farmFirm;
                firm.principal = principal;
                firm.NumberOfFields = NumberOfFields;
                return await FirmR.UpdateAsync(firm);

            }
            catch (Exception ex)
            {
                logger.LogError("【养殖服务层】修改企业部分信息 出错"+ ex.Message);
                throw;
            }
        }
        /// <summary>
        /// 显示企业信息
        /// </summary>
        /// <param name="LOT">批次</param>
        /// <returns>返回企业信息</returns>
       public async Task<Firm> ListFirm(string? LOT)
        {
            try
            {
                var firm = new Firm();
                var list = await FirmR.GetAsync(p => p.LOT == LOT);
                foreach (var item in list)
                {
                    firm = item;
                }

                return firm;

            }
            catch (Exception ex)
            {
                logger.LogError("【养殖服务层】修改企业部分信息 出错"+ ex.Message);
                throw;
            }
        }
    }
}
